﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Web.SessionState;

namespace Framework.Resources
{
    /// <summary>
    /// Summary description for GetFinance
    /// </summary>
    public class GetFinance : IHttpHandler, IReadOnlySessionState
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var status = 0;
            var userMember = Membership.GetUser();
            if (context.User.Identity.IsAuthenticated)
            {
                if (context.Request["stock"] != null)
                {
                    var code = context.Request["stock"].ToString();
                    var lstFinances = entity.dgs_Finances.Where(d => d.dgs_Stocks.Code == code).Select(d => new
                    {
                        Quarter = d.Quarter,
                        Year = d.Year,
                        ROE = d.ROE,
                        ROA = d.ROA,
                        EPS = d.EPS,
                        PE = d.PE,
                        PB = d.PB,
                        Revenue = d.Revenue,
                        NP = d.NP,
                        QR = d.QR,
                        TotalAssets = d.TotalAssets,
                        TotalLiabilities = d.TotalLiabilities,
                        KLLHBinhQuan = d.KLLHBinhQuan
                    });
                    var data = lstFinances.Where(d => d.Quarter.HasValue).OrderByDescending(d => d.Year).ThenByDescending(d => d.Quarter).Skip(0).Take(4).ToList();
                    data.AddRange(lstFinances.Where(d => d.Quarter.HasValue == false).OrderByDescending(d => d.Year).Skip(0).Take(3).ToList());
                    context.Response.Write(
                        jsonSerializer.Serialize(
                            new
                            {
                                Status = 1,
                                Data = data
                            })
                    );
                }
                else
                {
                    context.Response.Write(
                        jsonSerializer.Serialize(
                            new
                            {
                                Status = status
                            })
                    );
                }
            }
            else
            {
                context.Response.Write(
                        jsonSerializer.Serialize(
                            new
                            {
                                Status = status                                
                            })
                    );
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}